package model;

import java.util.Iterator;
import java.util.Vector;


public class MaterialList {
	

	public static MaterialList create(final Vector<QuantifiedComponent> materials){
		return new MaterialList(materials);
	}
	
	private final Vector<QuantifiedComponent> materials;
	
	protected MaterialList(final Vector<QuantifiedComponent> materials){
		this.materials = materials;
	}
	
	public Vector<QuantifiedComponent> getMaterials(){
		return this.materials;
	}
	
	
	public void multiply(int value){
		final Iterator<QuantifiedComponent> materials = this.getMaterials().iterator();
		while(materials.hasNext()){
			final QuantifiedComponent current = materials.next();
			current.multiplyQuantity(value);
		}
	}


	
	public void add(MaterialList list){
		final Iterator<QuantifiedComponent> materialsOfList = list.getMaterials().iterator();
		while(materialsOfList.hasNext()){
			final QuantifiedComponent currentOfList = materialsOfList.next();
			this.add(currentOfList);
		}
		
	}

	public void add(QuantifiedComponent quantifiedComponent){
		final Iterator<QuantifiedComponent> materials = this.getMaterials().iterator();
		while(materials.hasNext()){
			final QuantifiedComponent current = materials.next();
			if(current.getComponent().equals(quantifiedComponent.getComponent())){
				current.addQuantity(quantifiedComponent);
				return;
			} 
		}
		this.getMaterials().add(quantifiedComponent);
	}

	public int containsHowOften(Component component) {
		int result = 0;
		Iterator<QuantifiedComponent> parts = this.getMaterials().iterator();
		while(parts.hasNext()){
			QuantifiedComponent current = parts.next();
			result += current.containsHowOften(component);
		} return result;
	} 

	
		
}

